package com.example.esaclient;


import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.gson.Gson;

import android.app.Activity;
import android.os.AsyncTask;


public class RegisterCompany extends AsyncTask<Object, Integer, Boolean> {
 

	private String URL ;	
	private  static final String METHOD_NAME = "/RegisterCompany?";	
	NewAccountActivity caller;
	
	public RegisterCompany(Activity callerActivity){
		caller= (NewAccountActivity) callerActivity;
	}
	
	
	@Override
	protected Boolean doInBackground(Object... params) {
		
		
		 try {
		
			 GlobalSettings gs = new GlobalSettings(caller);
			 URL = "http://"+gs.getPrefString(gs.PREF_ESA_URL)+"/ServiceRest.svc";			 
			 
			 String url =URL+METHOD_NAME+"email="+params[0]+"&password="+params[1]+"&logo="+"&name="+params[2]+"&description="+params[3];
					  
			 HttpClient client = new DefaultHttpClient();
		        HttpGet httpGet = new HttpGet(url);
		        try {
		            HttpResponse response = client.execute(httpGet);
		            HttpEntity entity = response.getEntity();
		             
		            if(entity != null) {
		                InputStream stream = entity.getContent();
		                String responseString = GlobalSettings.convertStreamToString(stream);
		                
		                
		                   Wrapper w = new Gson().fromJson(responseString, Wrapper.class);
		                   return w.RegisterCompanyResult;
		                   
		            }
		             
		        } catch (ClientProtocolException e) {
		            e.printStackTrace();
		            
		        } catch (IOException e) {
		            e.printStackTrace();
		           
		        }
		         
 	         
	      }
	     catch (final Exception e) {
	         e.printStackTrace();   
	         
	     }
		
		 return false;
	
	}
	
		     protected void onPostExecute(Boolean result) {
	    	 caller.esaResult(result);
	     }

	
		     public class Wrapper {
		    	 public Boolean RegisterCompanyResult=false;
		     }
}
